package com.a2.appa3.widget;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.getHongbaoBean;
import com.a2.appa3.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/26.
 */

public class HongbaoDiolog extends Activity {
    //展示红包
    private ImageView close_hb;
    private Button open_hb;
    private RelativeLayout come;
    //开启红包
    private RelativeLayout open;
    private TextView money;
    private TextView conform;
    private ImageView close;
    //红包id
    private String hb_id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hongbao_diolog);
        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        hb_id = getIntent().getStringExtra("id");
        close_hb = findViewById(R.id.close_hb);
        open_hb = findViewById(R.id.open_hb);
        come = findViewById(R.id.come);
        open = findViewById(R.id.open);
        money = findViewById(R.id.money);
        conform = findViewById(R.id.conform);
        close = findViewById(R.id.close_hb_02);

        come.setVisibility(View.VISIBLE);
        open.setVisibility(View.GONE);

        close_hb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        open_hb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始网络请求：
                HttpJsonUtils.getHttpApiInstance()
                        .get_hongbao(HttpAdress.getuseAdress() + "api/mobile/takeHongBao", hb_id, Info.Token)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CustomObserver<getHongbaoBean>() {
                            @Override
                            protected void onCustomNext(getHongbaoBean resBanner) {
                                //然后给红包设置值
                                if (resBanner.isStatus()) {
                                    come.setVisibility(View.GONE);
                                    open.setVisibility(View.VISIBLE);
                                    money.setText(resBanner.getAmount());
                                } else {
                                    Toast.makeText(HongbaoDiolog.this, resBanner.getMsg() + "", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            protected void onSubscribeCustom(Disposable disposable) {

                            }

                            @Override
                            protected void onErrorCustom(Throwable e) {
                                SYSOUtils.sOut(e.toString());
                            }
                        });
            }
        });

    }
}
